var App = getApp()
const list60 = [], list20 = [],list2 = [],hours=[],mins=[];
for (let i = 1; i <= 60; i++) {list60.push(i)}
for (let i = 1; i <= 20; i++) {list20.push(i)}
for (let i = 1; i <= 20; i++) {list2.push(i/10)}
for (let i = 0; i <= 23; i++) {
	if(i<10){
		hours.push('0'+i)
	}else{
		hours.push(i)
	}
}
for (let i = 0; i <= 59; i++) {
	if(i<10){
		mins.push('0'+i)
	}else{
		mins.push(i)
	}
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    role_id:wx.getStorageSync('role_id'),
    domain:App.globalData.iconsUrl,
		list:[],
		list60,
		list20,
		list2,
		value: [],
		hours,
		mins,
		value1:[],
		// 弹窗
		show: false,
		picker_type:0,//1标准时段，2时段设置，3时段设置，优惠时段收费标准，4非会员收费标准
		store_id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取数据列表
    this.getDataList();
  },
  //获取数据列表
  getDataList(){
    let that = this;
    let obj = {b_id:App.get_b_id()}
    App.request('/Manage/Management/settingStoreList',obj,function(e){
      wx.stopPullDownRefresh()
        if (e.code == "200") {
          that.setData({list:e.result})
        } 
    })
  },
  // 提交保存
  submit(e){
			let store_id =e.currentTarget.dataset.id
			let that = this
			let obj = {}
			let list = that.data.list
			list.forEach(function(element) {
				if(element.store_id==store_id){
					obj = {...element}
				}
			})
      App.request('/Manage/Management/setStandard',obj,function(e){
          if (e.code == "200") {
            App.showSuccessToast(e.msg, function() {
              that.getDataList()          //刷新列表
            });
          } 
      })
	},
	// 显示弹窗
	picker_show(e){
		var list = this.data.list
		var store_id = e.currentTarget.dataset.id
		let picker_type = e.currentTarget.dataset.type
		var value = [0,0,0,0,0,0]
		var value1 =[0,0,0,0,0,0,0]
		let that = this
		list.forEach(function(element) {
			if(element.store_id==store_id){
				if(picker_type==1){
					value[1]=parseInt(that.getIndex(element.first_min,that.data.list60))
					value[3]=parseInt(that.getIndex(element.first_price,that.data.list20))
					value[5]=parseInt(that.getIndex(element.next_price,that.data.list2))
				}
				if(picker_type==2){
					let h = element.cheap_start.split(":")
					value1[0]=that.getIndex(h[0],that.data.hours)
					value1[2]=that.getIndex(h[1],that.data.mins)
					h = element.cheap_end.split(":")
					value1[4]=that.getIndex(h[0],that.data.hours)
					value1[6]=that.getIndex(h[1],that.data.mins)
				}
				if(picker_type==3){
					value[1]=parseInt(that.getIndex(element.cheap_min,that.data.list60))
					value[3]=parseInt(that.getIndex(element.cheap_first,that.data.list20))
					value[5]=parseInt(that.getIndex(element.cheap_next,that.data.list2))
				}
				if(picker_type==4){
					value[1]=parseInt(that.getIndex(element.nomember_min,that.data.list60))
					value[3]=parseInt(that.getIndex(element.nomember_first,that.data.list20))
					value[5]=parseInt(that.getIndex(element.nomembe_next,that.data.list2))
				}

			}
		});
		this.setData({
			show:true,
			picker_type,
			value,
			value1,
			store_id
		})
	},
	// 隐藏弹窗
	exit(){
		this.setData({show:false,picker_type:0})
	},
	switchChange(e){
		console.log(e)
		var list = this.data.list
		let store_id = e.currentTarget.dataset.id
		let that = this
		list.forEach(function(element,index) {
			if(element.store_id==store_id){
				list[index].is_cheap=e.detail.value?1:0
			}
		})
		that.setData({list})
	},
	// 触发选择
	bindChange(e){
		let store_id = this.data.store_id
		var list = this.data.list
		let that = this
		let value = e.detail.value
		list.forEach(function(element,index) {
			if(element.store_id==store_id){
				if(that.data.picker_type==1){
					list[index].first_min = that.data.list60[value[1]]
					list[index].first_price=that.data.list20[value[3]].toFixed(2)
					list[index].next_price =that.data.list2[value[5]].toFixed(2)
				}
				if(that.data.picker_type==2){
					list[index].cheap_start = that.data.hours[value[0]]+':'+that.data.mins[value[2]]
					list[index].cheap_end   = that.data.hours[value[4]]+':'+that.data.mins[value[6]]
				}
				if(that.data.picker_type==3){
					list[index].cheap_min = that.data.list60[value[1]]
					list[index].cheap_first=that.data.list20[value[3]].toFixed(2)
					list[index].cheap_next =that.data.list2[value[5]].toFixed(2)
				}
				if(that.data.picker_type==4){
					list[index].nomember_min = that.data.list60[value[1]]
					list[index].nomember_first=that.data.list20[value[3]].toFixed(2)
					list[index].nomembe_next =that.data.list2[value[5]].toFixed(2)
				}
			}
		});
		that.setData({list})
	},

	// 根据value获取在对象中的index
	getIndex(v,obj){
		for (const key in obj) {
			if (obj.hasOwnProperty(key) && obj[key]==v) { // 确保属性是对象自己的，不是继承的
					return key
			}
		}
		return 0
	},
	moneyFormat(v){

	},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getDataList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})